package com.honey.solr;

import org.java.plugin.Plugin;
import org.java.plugin.PluginLifecycleException;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.InnerClass;
import com.honey.compilation.java.InnerEnum;
import com.honey.compilation.java.JavaVisibility;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.StaticBlock;
import com.honey.core.CorePlugin;
import com.honey.core.Extension;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.ContentType;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.notify.Signal;
import com.honey.core.types.ExceptionFullyQualifiedJavaType;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.utils.ObjectFactory;
import com.honey.generator.java.extension.point.GeneratorJavaClass;
import com.honey.solr.daosupport.field.AndKeyWordField;
import com.honey.solr.daosupport.field.HighlightField;
import com.honey.solr.daosupport.field.HighlightSimplePostField;
import com.honey.solr.daosupport.field.HighlightSimplePreField;
import com.honey.solr.daosupport.field.IsAutoCommitField;
import com.honey.solr.daosupport.field.NotKeyWordField;
import com.honey.solr.daosupport.field.OrKeyWordField;
import com.honey.solr.daosupport.field.SolrServerField;
import com.honey.solr.daosupport.field.SolrServerUrlField;
import com.honey.solr.daosupport.method.Add1Method;
import com.honey.solr.daosupport.method.Add2Method;
import com.honey.solr.daosupport.method.Add3Method;
import com.honey.solr.daosupport.method.Add4Method;
import com.honey.solr.daosupport.method.AddBean1Method;
import com.honey.solr.daosupport.method.AddBean2Method;
import com.honey.solr.daosupport.method.AddBeans1Method;
import com.honey.solr.daosupport.method.AddBeans2Method;
import com.honey.solr.daosupport.method.Between1Method;
import com.honey.solr.daosupport.method.BetweenMethod;
import com.honey.solr.daosupport.method.Commit1Method;
import com.honey.solr.daosupport.method.Commit2Method;
import com.honey.solr.daosupport.method.ConditionAdapterMethod;
import com.honey.solr.daosupport.method.DeleteById1Method;
import com.honey.solr.daosupport.method.DeleteById2Method;
import com.honey.solr.daosupport.method.DeleteById3Method;
import com.honey.solr.daosupport.method.DeleteById4Method;
import com.honey.solr.daosupport.method.DeleteById5Method;
import com.honey.solr.daosupport.method.DeleteById6Method;
import com.honey.solr.daosupport.method.DeleteById7Method;
import com.honey.solr.daosupport.method.GreaterMethod;
import com.honey.solr.daosupport.method.In1Method;
import com.honey.solr.daosupport.method.InMethod;
import com.honey.solr.daosupport.method.LessMethod;
import com.honey.solr.daosupport.method.Like1Method;
import com.honey.solr.daosupport.method.LikeMethod;
import com.honey.solr.daosupport.method.NotBetweenMethod;
import com.honey.solr.daosupport.method.NotInMethod;
import com.honey.solr.daosupport.method.NotLikeMethod;
import com.honey.solr.daosupport.method.NotMethod;
import com.honey.solr.daosupport.method.Optimize1Method;
import com.honey.solr.daosupport.method.Optimize2Method;
import com.honey.solr.daosupport.method.Optimize3Method;
import com.honey.solr.daosupport.method.PingMethod;
import com.honey.solr.daosupport.method.Query1Method;
import com.honey.solr.daosupport.method.Query3Method;
import com.honey.solr.daosupport.method.Query4Method;
import com.honey.solr.daosupport.method.Query5Method;
import com.honey.solr.daosupport.method.Query6Method;
import com.honey.solr.daosupport.method.RollbackMethod;
import com.honey.solr.daosupport.method.SetQueryParameterMethod;
import com.honey.solr.daosupport.method.SetSolrServerUrlMethod;
import com.honey.solr.daosupport.method.SpringMethod;

public class SolrExtension extends Extension<Plugin> implements GeneratorJavaClass {
	
	private SolrPlugin solrPlugin = null;
	
	private SolrFactory solrFactory = null;
	
	private AbstractJavaCompilation abstractJavaCompilation ;
	
	public SolrExtension(Plugin belongPlugin) {
		super(belongPlugin);
		solrPlugin = ( SolrPlugin ) belongPlugin;
		solrFactory = new SolrFactory(solrPlugin);
	}

	@Override
	public void clean() {
		
	}

	@Override
	public AbstractJavaCompilation createNewCompilation(
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		
		try {
			Clazz clazz = new Clazz(solrPlugin.getTargetPackage()+"."+ solrPlugin.getTargetClassName());
			clazz.addClassComment("Solr全文检索API接口支持.使用者需要将solrj的包导入到classpath中. ");
			clazz.addClassComment("Solrj version : 3.6 ");
			clazz.addClassComment("Solr version  : 3.6 ");
			clazz.setAutoAddGetterMethod(true);
			clazz.setAutoAddSetterMethod(true);
			abstractJavaCompilation =  clazz;
			
			new SpringMethod(solrFactory).snedBeanSignalToSpringPlugin();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return abstractJavaCompilation;
	}

	@Override
	public Method[] importMethods() {
		return new Method[]{
			(new Add1Method()).createMethod(),	
			(new Add2Method()).createMethod(),
			(new Add3Method()).createMethod(),
			(new Add4Method()).createMethod(),
			(new AddBean1Method()).createMethod(),
			(new AddBean2Method()).createMethod(),
			(new AddBeans1Method()).createMethod(),
			(new AddBeans2Method()).createMethod(),
			(new DeleteById1Method()).createMethod(),
			(new DeleteById2Method()).createMethod(),
			(new DeleteById3Method()).createMethod(),
			(new DeleteById4Method()).createMethod(),
			(new DeleteById5Method()).createMethod(),
			(new DeleteById6Method()).createMethod(),
			(new DeleteById7Method(this.solrFactory)).createMethod(),
			//(new Query1Method()).createMethod(),
			//(new Query2Method()).createMethod(),
			(new Query3Method(this.solrFactory)).createMethod(),
			(new Query4Method(solrPlugin)).createMethod(),
			(new Query5Method(this.solrFactory)).createMethod(),
			(new Query6Method(this.solrFactory)).createMethod(),
			(new SetQueryParameterMethod(this.solrFactory)).createMethod(),
			
			(new Optimize1Method()).createMethod(),
			(new Optimize2Method()).createMethod(),
			(new Optimize3Method()).createMethod(),
			(new Commit1Method()).createMethod(),
			(new Commit2Method()).createMethod(),
			(new RollbackMethod()).createMethod(),
			(new PingMethod()).createMethod(),
			(new ConditionAdapterMethod( solrFactory )).createMethod(),
			
			
			(new SetSolrServerUrlMethod(solrPlugin,abstractJavaCompilation)).createMethod(),
		};
	}

	
	@Override
	public Field[] importFields() {
		return new Field[]{
			(new NotKeyWordField()).createField(),
			(new OrKeyWordField()).createField(),
			(new AndKeyWordField()).createField(),
			(new SolrServerUrlField()).createField(),
			(new SolrServerField()).createField(),
			(new IsAutoCommitField()).createField(),
			(new HighlightField()).createField(),
			(new HighlightSimplePreField()).createField(),
			(new HighlightSimplePostField()).createField(),
		};
	}

	@Override
	public FullyQualifiedJavaType[] importImportClass() {
		return new FullyQualifiedJavaType[]{
				ExceptionFullyQualifiedJavaType.getIOExceptionInstance(),
				solrFactory.getGeneratedCriteriaType(),
		};
	}

	@Override
	public InnerClass[] importInnerClass() {
		InnerClass[] answer = null;
		try {
			InnerClass inner = new InnerClass(new FullyQualifiedJavaType(SolrConstant.LUCENE_QUERY_ADAPTER));
			inner.addAnnotation("@SuppressWarnings({ \"unchecked\", \"rawtypes\" })");
			inner.getClassDecoration().setVisibility(JavaVisibility.PRIVATE);
			
			inner.addMethod((new GreaterMethod(solrFactory)).createMethod());
			inner.addMethod((new LessMethod(solrFactory)).createMethod());
			inner.addMethod((new Like1Method(solrFactory)).createMethod());
			inner.addMethod((new In1Method(solrFactory)).createMethod());
			inner.addMethod((new Between1Method(solrFactory)).createMethod());
			
			inner.addMethod((new NotLikeMethod(solrFactory)).createMethod());
			inner.addMethod((new NotInMethod(solrFactory)).createMethod());
			inner.addMethod((new NotBetweenMethod(solrFactory)).createMethod());
			
			inner.addMethod((new NotMethod()).createMethod());
			inner.addMethod((new LikeMethod()).createMethod());
			inner.addMethod((new InMethod()).createMethod());
			inner.addMethod((new BetweenMethod()).createMethod());
			
			answer = new InnerClass[] {inner};
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		
		return answer;
	}

	@Override
	public InnerEnum[] importInnerEnumClass() {
		return null;
	}

	@Override
	public StaticBlock[] importStaticBlocks() {
		return null;
	}
	
	@Override
	public ContentType getContentType() {
		return null;
	}


	@Override
	public FullyQualifiedJavaType getType() {
		return null;
	}

	public void snedSignal(Signal signal ) {
		try {
			CorePlugin plugin = (CorePlugin)ObjectFactory.getRequestPlugin(solrPlugin, "honey.core.plugin") ;
			plugin.notify(signal);
		} catch (PluginLifecycleException e) {
			e.printStackTrace();
		}
	}
}
